package com.lolineet.auth.controller;

import com.lolineet.auth.entity.SysUsernamePasswordLoginEntity;
import com.lolineet.auth.service.JwtService;
import com.lolineet.auth.token.SysUsernamePasswordAuthToken;
import com.lolineet.result.Result;
import com.lolineet.result.ResultCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * AuthController
 *
 * @author YUKI.N
 * @version 1.0
 * @description
 * @date 2025/4/17 22:27
 */
@Tag(name = "认证相关接口")
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Resource
    private JwtService jwtService;

    @Resource
    private AuthenticationManager authenticationManager;

    @PostMapping("/login")
    @Operation(summary = "登录接口")
    public Result login(@RequestBody SysUsernamePasswordLoginEntity loginEntity) {
        SysUsernamePasswordAuthToken authToken = new SysUsernamePasswordAuthToken(loginEntity, loginEntity.getPassword());
        Authentication authentication = authenticationManager.authenticate(authToken);
        String token = jwtService.createToken(authentication);
        Result<String> result = new Result<String>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(token);
        return result;
    }
}
